/**
 * Title:        流程版本信息表数据检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

import org.json.JSONObject;

import weaver.email.EmailEncoder;
import weaver.email.Email_Autherticator;
import weaver.general.GCONST;
import weaver.general.OrderProperties;
import weaver.general.Util;
import weaver.system.SystemComInfo;


public class CheckMail extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();

        List resultList = new ArrayList();
        String result = "";
        
        String imagefilename = "/images/hdMaintenance.gif";
        String needfav ="1";
        String needhelp ="";
        String sendneedSSL = "0";
        String getneedSSL = "0";
        String accountName="", accountMailAddress="", accountId="",accountPassword="",  popServer="", smtpServer="", needCheck="", needSave="",autoreceive="";
        int serverType=1, smtpServerPort=25;
        String encryption="";

        SystemComInfo systemComInfo = new SystemComInfo();
        needCheck=systemComInfo.getDefneedauth();
        accountMailAddress = systemComInfo.getDefmailfrom();
        accountId = systemComInfo.getDefmailuser();
        accountPassword = systemComInfo.getDefmailpassword();
        smtpServer=systemComInfo.getDefmailserver();
        sendneedSSL =systemComInfo.getNeedSSL();
        smtpServerPort=Integer.parseInt(systemComInfo.getSmtpServerPort().trim());


        if(sendneedSSL.equals("1")|| getneedSSL.equals("1")){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage","不能使用ssl发送方式!");
            resultMap.put("linkurl","/email/maint/MailSystemFrame.jsp");
            resultMap.put("linkname","请点击链接进行设置。");
            resultList.add(resultMap);
            return formatReturnJsonCheck(result, resultList,this);
        }
        if ("1".equals(needCheck)){
            needCheck="true";
        }else{
            needCheck="false";
        }
        if(encryption.equals("1")) accountPassword = EmailEncoder.DecoderPassword(accountPassword);

        String pstate="初始化值"; 
        boolean sport=false;
        boolean pport=false;
        boolean p=false;

        boolean  sendstart=true;
        String  sendString="未测试发送";



        String sstate="初始化值"; 

        boolean s=false;
        try{
        InetAddress saddress = InetAddress.getByName(smtpServer);

            s=saddress.isReachable(2000);
        }catch(Exception e){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage","发送测试失败:"+e+"\n"+e.getMessage());
            resultMap.put("linkurl","/email/maint/MailSystemFrame.jsp");
            resultMap.put("linkname","请点击链接进行设置。");
            resultList.add(resultMap);
            s=false;
            return formatReturnJsonCheck(result, resultList,this);
        }



        if(true){
            try {
                Socket socket = new Socket(smtpServer, smtpServerPort);
                sport=true;
                sstate="通讯正常!";
            } catch (UnknownHostException e) {
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","无效地址!");
                resultMap.put("linkurl","/email/maint/MailSystemFrame.jsp");
                resultMap.put("linkname","请点击链接进行设置。");
                resultList.add(resultMap);
                return formatReturnJsonCheck(result, resultList,this);
            } catch (IOException e) {
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","端口连不通!");
                resultMap.put("linkurl","/email/maint/MailSystemFrame.jsp");
                resultMap.put("linkname","请点击链接进行设置。");
                resultList.add(resultMap);
                return formatReturnJsonCheck(result, resultList,this);
            }catch (Exception e) {
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","未知错误:"+e+"/n"+e.getMessage());
                resultMap.put("linkurl","/email/maint/MailSystemFrame.jsp");
                resultMap.put("linkname","请点击链接进行设置。");
                resultList.add(resultMap);
                return formatReturnJsonCheck(result, resultList,this);
            }
        }else{
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage","该地址连不通!");
            resultMap.put("linkurl","/email/maint/MailSystemFrame.jsp");
            resultMap.put("linkname","请点击链接进行设置。");
            resultList.add(resultMap);
            return formatReturnJsonCheck(result, resultList,this);
        }
        if(sport){
             String port = ""+smtpServerPort;
             Date d= new Date();
             String server = smtpServer;//邮件服务器

             String from = accountId;//发送者,显示的发件人名字

             String user = accountMailAddress;//发送者邮箱地址

             String password = accountPassword;//密码
             
             
             String email="";

             email=accountMailAddress;
             
            
             
             String subject="mailtest:"+d;
             
             String body="邮件发送测试";
             
             try {
                server = new String(server.getBytes("ISO8859-1"),"GBK");
                from = new String(from.getBytes("ISO8859-1"),"GBK");
                user = new String(user.getBytes("ISO8859-1"),"GBK");
                password = new String(password.getBytes("ISO8859-1"),"GBK");
                email = new String(email.getBytes("ISO8859-1"),"GBK");
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
             

                 try {
                     Properties props = new Properties();
                     props.put("mail.smtp.host", server);
                     props.put("mail.smtp.port", port);
                     props.put("mail.smtp.localhost", "localhost");
                     javax.mail.Authenticator auth = null;           
                     if ("true".equals(needCheck)) {
                         props.put("mail.smtp.auth", "true");
                         auth = new Email_Autherticator(accountId, accountPassword);
                     }
                     
                     Transport transport = null;
                     javax.mail.Session session1 =null;
                     if(!sendneedSSL.equals("1"))
                         session1 = Session.getInstance(props, auth);
                     else{
                         result = resultNeedFix;
                         resultList.add("<h1>不能使用ssl发送方式!</h1>");
                         return formatReturnJsonCheck(result, resultList,this);
                     
                     }
                
                     javax.mail.internet.MimeMessage msg = new javax.mail.internet.MimeMessage(session1);
                     
                     javax.mail.internet.InternetAddress fromAddress = new  javax.mail.internet.InternetAddress(user,from,"UTF-8");
                     msg.setFrom(fromAddress);
                     msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
                     msg.setSubject(subject, "UTF-8");   
                     msg.setText(body, "UTF-8");
                     msg.saveChanges();
                     Transport.send(msg);
                     sendString="发送到"+email+"测试成功";
                     result = resultOk;
                     return formatReturnJsonCheck(result, resultList,this);
                 } catch (Exception e) {
                     result = resultNeedFix;
                     sendString=sendString+"\n"+"发送测试失败:"+e+"\n"+e.getMessage();
                     sendstart= false;
                     e.printStackTrace();
                     return formatReturnJsonCheck(result, resultList,this);
                     
                 } 
        }
        (new BaseBean()).writeLog("CheckOvertime  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "邮件发送测试";
    }

    @Override
    public String getClassDesc() {
        // TODO Auto-generated method stub
        return "检测邮件是否能够发送成功";
    }

}



